<{include file="admin/public/header.html"}>
<div class="am-cf admin-main">
  <!-- sidebar start -->
  <{include file='admin/public/left.html'}>
  <!-- sidebar end -->

  <!-- content start -->
  <div class="admin-content">

    <div class="am-cf am-padding">
      <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg"><{$nav[0]}></strong> / <small><{$nav[1]}></small></div>
      <div class="am-alert am-alert-success am-u-end success-msg" id="success-msg">添加成功！</div>
    </div>

    <div class="am-g">
      <div class="am-u-sm-12 am-u-md-6">
        <div class="am-btn-toolbar">
          <div class="am-btn-group am-btn-group-xs">
          </div>
        </div>
      </div>
    </div>

    <div class="am-g">
      <div class="am-u-sm-12">
        <form class="am-form" id="doc-vld-msg">
          <table class="am-table am-table-striped ">
          <tbody>
            <{foreach from=$list item="val"}>
            <tr>
              <td>
              <input type="checkbox" name="check[]" id="check<{$val.id}>" value="<{$val.id}>" onclick="check(<{$val.id}>, 0)"  <{if in_array($val.id,$auth)}>checked=checked<{/if}>/>
              <span><strong><{$val.title}></strong></span>
              </td>
            </tr>
            <{foreach from=$val.child item="va"}>
            <tr>
              <td>
              &nbsp;&nbsp;&nbsp;
              <input type="checkbox" name="check[]" id="check<{$va.id}>" value="<{$va.id}>" parent="<{$val.id}>" onclick="check(<{$va.id}>, 1)" <{if in_array($va.id,$auth)}>checked<{/if}>/>
              <{$va.title}>
              </td>
              <{foreach from=$va.child item="v"}>
              <td>
              <input type="checkbox" name="check[]" id="check<{$v.id}>" value="<{$v.id}>" parent="<{$va.id}>" onclick="check(<{$v.id}>, 2)"  <{if in_array($v.id,$auth)}>checked<{/if}>/>
              <{$v.title}>
              </td>
              <{/foreach}>
            </tr>
            <{/foreach}>
            <{/foreach}>
            
          </tbody>
        </table>
        <{if $data}>
        <input name="id" type="hidden" class="am-input-sm" value="<{$data.id}>">
        <{/if}>
        </form>
        <div class="am-margin">
          <button id="save-sure" type="submit" class="am-btn am-btn-primary am-btn-xs">提交保存</button>
          <button id="save-forgive" type="button" class="am-btn am-btn-primary am-btn-xs" onclick="jumpUrl('/admin/role/index')">放弃保存</button>
        </div>
      </div>

    </div>
  </div>
  <!-- content end -->
</div>
<{include file='admin/public/footer.html'}>
<script>
  $('#save-sure').on('click', function() {
    var postData = $('#doc-vld-msg').serializeArray();
    
    var url = "/admin/role/distribute";
    var jumpUrl = '/admin/role/index';
    post(url, postData, jumpUrl);
  });

  function check(id, step)
  {
    if (step == 0) 
    {
      var check = $('input[parent="' + id + '"]');
      checkChild(id);
      check.each(function(){
        checkChild($(this).attr('value'));
      });
    }
    else if (step == 1)
    {
      checkChild(id);
      checkParent(id);
    }
    else if (step == 2)
    {
      var parentId = $('#check' + id).attr('parent');
      checkParent(id);
      checkParent(parentId);
      
    }
  }

  function checkParent(id)
  {
    var parentId = $('#check' + id).attr('parent');
    var sameCheck = $('input[parent="' + parentId + '"]');
    if ($('#check' + id).is(':checked') == false) 
    {
      var res = false;
      sameCheck.each(function(){
        if ($('#check' + $(this).attr('value')).is(':checked') == true) 
        {
          res = true;
        }
      });
      if (res == false) 
      {
          $('#check' + parentId).prop('checked',false);
      };
    }
    else
    {
      $('#check' + parentId).prop('checked',true);
    }
  }

  function checkChild(id)
  {
    var check = $('input[parent="' + id + '"]');
    if ($('#check' + id).is(':checked') == false) 
    {
      check.prop('checked',false);
    }
    else
    {
      check.prop('checked',true);
    }
  }

</script>
</body>
</html>
